package page;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import ressource.Message;

/**
 * Servlet implementation class FAQServlet
 */
public class FAQServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FAQServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dis;
		Connection con = null;
		
		try{
			Context namingContext;
			PreparedStatement pstmt;
			ResultSet rs;
			
			namingContext = new InitialContext();
			DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
			con = dataSource.getConnection();
			
			//Maintenant nous gérons le dialogue autour de l'annonce si il y en a un :
			pstmt=con.prepareStatement("SELECT u.avatar, m.id_message, m.date, m.message, u.pseudo, u.id_membre FROM message_annonce m, membre u WHERE m.id_annonce = 0 AND m.id_membre = u.id_membre ORDER BY m.date DESC LIMIT 0, 20");
			
			rs = pstmt.executeQuery();
			
			//Les objets qui vont nous servir :
			Message m;
			List<Message> messages = new ArrayList<Message>();
			Integer cpt = 0;
			while(rs.next()) {
				m = new Message();
				m.setDate(rs.getTimestamp("date"));
				m.setMessage(rs.getString("message"));
				m.setPseudoMembre(rs.getString("pseudo"));
				m.setIdAuteur(rs.getInt("id_membre"));
				if(++cpt % 2 == 0) m.setCouleur("#CCCCCC");
				m.setIdMessage(rs.getInt("id_message"));
				m.setAvatarMembre(rs.getString("avatar"));
				messages.add(m);
			}
			request.setAttribute("messages", messages);
			
			dis = request.getRequestDispatcher("faq.jsp");
			dis.forward(request, response);
		}catch(NamingException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendRedirect("faq.jsp");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendRedirect("faq.jsp");
		} finally {
			try {
				if(con != null) con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
